﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using GovernmentOfficeProject.LevelDataAccessModel;
using System.Data;
using System.Data.SqlClient;
using GovernmentOfficeProject.SysTools;
using GovernmentOfficeProject.SysTools;
using System.Text;


namespace GovernmentOfficeProject.LevelDataAccess
{
    public class DAL_USER_User
    {
        public DAL_USER_User()
        {
        }


        /// <summary>
        /// 根据用户ID,获取指定用户信息.
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public Entity_USER_User getUserByUserID(decimal userID)
        {
            string strSQL = "";
            DataSet dst = null;
            Entity_USER_User user = null;
            try
            {
                //user = new Entity_USER_User();
                strSQL = "select * from [PER_USER] where 1=1";
                if (userID != -1)
                {
                    strSQL += " and UserID=" + userID + "";
                }    
                dst = DAL_DBGM.GetDataSet(strSQL);
                foreach (DataRow row in dst.Tables[0].Rows)
                {
                    user = new Entity_USER_User();
                    user.UserID = Convert.ToDecimal(row["UserID"]);
                    user.LoginName = Convert.ToString(row["UserLoginName"]);
                    user.LoginPassword = Convert.ToString(row["UserLoginPassword"]);
                    user.RealName = Convert.ToString(row["UserRealName"]);
                    user.Sex = Convert.ToString(row["UserSex"]);
                    user.MobilePhone = Convert.ToString(row["UserMobilePhone"]);
                    user.Telephony = Convert.ToString(row["UserTelephony"]);
                    user.Email = Convert.ToString(row["UserEmail"]);
                    user.QQ = Convert.ToString(row["UserQQ"]);
                    user.OfficeAddress = Convert.ToString(row["UserOfficeAddress"]);
                    user.HomeAddress = Convert.ToString(row["UserHomeAddress"]);
                    user.LastLoginTime = Convert.ToString(row["UserLastLoginTime"]);
                    user.CreateUserID = Convert.ToDecimal(row["UserCreateUserID"]);
                    user.CreateDateTime = Convert.ToString(row["UserCreateDate"]);
                    user.ModifyUserID = Convert.ToDecimal(row["UserModifyUserID"]);
                    user.ModifyDateTime = Convert.ToString(row["UserModifyDate"]);
                    user.RecordStatus = Convert.ToDecimal(row["UserRecordStatus"]);
                    user.Backup1 = Convert.ToString(row["UserBackup1"]);
                    user.Backup2 = Convert.ToString(row["UserBackup2"]);
                    user.Backup3 = Convert.ToString(row["UserBackup3"]);
                    user.Backup4 = Convert.ToString(row["UserBackup4"]);
                    user.Backup5 = Convert.ToString(row["UserBackup5"]);
                }
                return user;
            }
            catch (Exception ex)
            {
                AppLog.Write("---异常位置:LevelDataAccess.DAL_USER.getUserByUserID---", AppLog.LogMessageType.Error, ex);
                throw ex;
            }
        }


        /// <summary>
        /// 根据部门ID,获取该部门下所有用户.
        /// </summary>
        /// <param name="deptID"></param>
        /// <returns></returns>
        public List<Entity_USER_User> getUserListByDeptID(decimal deptID)
        {
            try
            {
                //string strSQL = "select * from [PER_DEPARTMENT] where DeptID = @deptID";
                string strSQL = "select A.* from [PER_USER] A,[PER_DEPARTMENT] B,[PER_USER_DEPARTMENT] M WHERE A.UserID = M.UDUserID AND B.DeptID = M.UDDEPTID	AND M.UDDEPTID = @deptID";
                SqlParameter[] values = new SqlParameter[]{
                    new SqlParameter("@deptID",deptID)
                };
                DataSet dst = DAL_DBGM.GetDataSet(strSQL, values);
                List<Entity_USER_User> userList = new List<Entity_USER_User>();
                Entity_USER_User user = null;
                foreach (DataRow row in dst.Tables[0].Rows)
                {
                    user = new Entity_USER_User();
                    user.UserID = Convert.ToDecimal(row["UserID"]);
                    user.LoginName = Convert.ToString(row["UserLoginName"]);
                    user.LoginPassword = Convert.ToString(row["UserLoginPassword"]);
                    user.RealName = Convert.ToString(row["UserRealName"]);
                    user.Sex = Convert.ToString(row["UserSex"]);
                    user.MobilePhone = Convert.ToString(row["UserMobilePhone"]);
                    user.Telephony = Convert.ToString(row["UserTelephony"]);
                    user.Email = Convert.ToString(row["UserEmail"]);
                    user.QQ = Convert.ToString(row["UserQQ"]);
                    user.OfficeAddress = Convert.ToString(row["UserOfficeAddress"]);
                    user.HomeAddress = Convert.ToString(row["UserHomeAddress"]);
                    user.LastLoginTime = Convert.ToString(row["UserLastLoginTime"]);
                    user.CreateUserID = Convert.ToDecimal(row["UserCreateUserID"]);
                    user.CreateDateTime = Convert.ToString(row["UserCreateDate"]);
                    user.ModifyUserID = Convert.ToDecimal(row["UserModifyUserID"]);
                    user.ModifyDateTime = Convert.ToString(row["UserModifyDate"]);
                    user.RecordStatus = Convert.ToDecimal(row["UserRecordStatus"]);
                    user.Backup1 = Convert.ToString(row["UserBackup1"]);
                    user.Backup2 = Convert.ToString(row["UserBackup2"]);
                    user.Backup3 = Convert.ToString(row["UserBackup3"]);
                    user.Backup4 = Convert.ToString(row["UserBackup4"]);
                    user.Backup5 = Convert.ToString(row["UserBackup5"]);
                    userList.Add(user);
                }
                return userList;
            }
            catch (Exception ex)
            {
                AppLog.Write("---异常位置:LevelDataAccess.DAL_USER_User.getUserListByDeptID(decimal deptID)---", AppLog.LogMessageType.Error, ex);
                throw ex;
            }
        }

        /// <summary>
        /// 获得关联用户列表
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public List<Entity_USER_User> getUserListByRelation(decimal userId)
        {
            try
            {
                string sql = @"select * from PER_USER T WHERE T.UserID IN (SELECT DISTINCT R.URControlledUserID FROM PER_USER_RELATION R WHERE R.URMasterUserID = {0})".FormatWith(userId);
                DataSet dst = DAL_DBGM.GetDataSet(sql);
                List<Entity_USER_User> userList = new List<Entity_USER_User>();
                Entity_USER_User user = null;
                foreach (DataRow row in dst.Tables[0].Rows)
                {
                    user = new Entity_USER_User();
                    user.UserID = Convert.ToDecimal(row["UserID"]);
                    user.LoginName = Convert.ToString(row["UserLoginName"]);
                    user.LoginPassword = Convert.ToString(row["UserLoginPassword"]);
                    user.RealName = Convert.ToString(row["UserRealName"]);
                    user.Sex = Convert.ToString(row["UserSex"]);
                    user.MobilePhone = Convert.ToString(row["UserMobilePhone"]);
                    user.Telephony = Convert.ToString(row["UserTelephony"]);
                    user.Email = Convert.ToString(row["UserEmail"]);
                    user.QQ = Convert.ToString(row["UserQQ"]);
                    user.OfficeAddress = Convert.ToString(row["UserOfficeAddress"]);
                    user.HomeAddress = Convert.ToString(row["UserHomeAddress"]);
                    user.LastLoginTime = Convert.ToString(row["UserLastLoginTime"]);
                    user.CreateUserID = Convert.ToDecimal(row["UserCreateUserID"]);
                    user.CreateDateTime = Convert.ToString(row["UserCreateDate"]);
                    user.ModifyUserID = Convert.ToDecimal(row["UserModifyUserID"]);
                    user.ModifyDateTime = Convert.ToString(row["UserModifyDate"]);
                    user.RecordStatus = Convert.ToDecimal(row["UserRecordStatus"]);
                    user.Backup1 = Convert.ToString(row["UserBackup1"]);
                    user.Backup2 = Convert.ToString(row["UserBackup2"]);
                    user.Backup3 = Convert.ToString(row["UserBackup3"]);
                    user.Backup4 = Convert.ToString(row["UserBackup4"]);
                    user.Backup5 = Convert.ToString(row["UserBackup5"]);
                    userList.Add(user);
                }
                return userList;
            }
            catch(Exception e)
            {
                AppLog.Write(e.Message, AppLog.LogMessageType.Error);
            }
            return null;
        }


        /// <summary>
        /// 获取所有用户信息.
        /// </summary>
        /// <returns></returns>
        public List<Entity_USER_User> getUserList()
        {
            try
            {
                string strSQL = "select * from [PER_USER]";
                DataSet dst = DAL_DBGM.GetDataSet(strSQL);
                List<Entity_USER_User> userList = new List<Entity_USER_User>();
                Entity_USER_User user = null;
                foreach (DataRow row in dst.Tables[0].Rows)
                {
                    user = new Entity_USER_User();
                    user.UserID = Convert.ToDecimal(row["UserID"]);
                    user.LoginName = Convert.ToString(row["UserLoginName"]);
                    user.LoginPassword = Convert.ToString(row["UserLoginPassword"]);
                    user.RealName = Convert.ToString(row["UserRealName"]);
                    user.Sex = Convert.ToString(row["UserSex"]);
                    user.MobilePhone = Convert.ToString(row["UserMobilePhone"]);
                    user.Telephony = Convert.ToString(row["UserTelephony"]);
                    user.Email = Convert.ToString(row["UserEmail"]);
                    user.QQ = Convert.ToString(row["UserQQ"]);
                    user.OfficeAddress = Convert.ToString(row["UserOfficeAddress"]);
                    user.HomeAddress = Convert.ToString(row["UserHomeAddress"]);
                    user.LastLoginTime = Convert.ToString(row["UserLastLoginTime"]);
                    user.CreateUserID = Convert.ToDecimal(row["UserCreateUserID"]);
                    user.CreateDateTime = Convert.ToString(row["UserCreateDate"]);
                    user.ModifyUserID = Convert.ToDecimal(row["UserModifyUserID"]);
                    user.ModifyDateTime = Convert.ToString(row["UserModifyDate"]);
                    user.RecordStatus = Convert.ToDecimal(row["UserRecordStatus"]);
                    user.Backup1 = Convert.ToString(row["UserBackup1"]);
                    user.Backup2 = Convert.ToString(row["UserBackup2"]);
                    user.Backup3 = Convert.ToString(row["UserBackup3"]);
                    user.Backup4 = Convert.ToString(row["UserBackup4"]);
                    user.Backup5 = Convert.ToString(row["UserBackup5"]);
                    userList.Add(user);
                }
                return userList;
            }
            catch (Exception ex)
            {
                AppLog.Write("---异常位置:LevelDataAccess.DAL_USER_User.getUserList()---", AppLog.LogMessageType.Error, ex);
                throw ex;
            }
        }


        /// <summary>
        /// 根据传入实体类对象的参数,进行多条件查找,并返回用户列表.
        /// </summary>
        /// <param name="userEntity">实体类传输对象</param>
        /// <returns>用户列表</returns>
        public List<Entity_USER_User> getUserListByUserEntity(Entity_USER_User userEntity)
        {
            try
            {
                string strSQL = "select * from [PER_USER] where 1=1";
                DataSet dst = null;
                if (userEntity.UserID != -1)
                {
                    strSQL += " and UserID=" + userEntity.UserID + "";
                }
                if (userEntity.LoginName != "" && userEntity.LoginName != null)
                {
                    strSQL += " and UserLoginName='" + userEntity.LoginName + "'";
                }
                if (userEntity.LoginPassword != "" && userEntity.LoginPassword != null)
                {
                    strSQL += " and UserLoginPassword='" + userEntity.LoginPassword + "'";
                }
                if (userEntity.RealName != "" && userEntity.RealName != null)
                {
                    strSQL += " and UserRealName='" + userEntity.RealName + "'";
                }
                if (userEntity.Sex != "" && userEntity.Sex != null)
                {
                    strSQL += " and UserSex='" + userEntity.Sex + "'";
                }
                if (userEntity.MobilePhone != "" && userEntity.MobilePhone != null)
                {
                    strSQL += " and UserMobilePhone='" + userEntity.MobilePhone + "'";
                }
                if (userEntity.Telephony != "" && userEntity.Telephony != null)
                {
                    strSQL += " and UserTelephony='" + userEntity.Telephony + "'";
                }
                if (userEntity.Email != "" && userEntity.Email != null)
                {
                    strSQL += " and UserEmail='" + userEntity.Email + "'";
                }
                if (userEntity.QQ != "" && userEntity.QQ != null)
                {
                    strSQL += " and UserQQ='" + userEntity.QQ + "'";
                }
                if (userEntity.OfficeAddress != "" && userEntity.OfficeAddress != null)
                {
                    strSQL += " and UserOfficeAddress='" + userEntity.OfficeAddress + "'";
                }
                if (userEntity.HomeAddress != "" && userEntity.HomeAddress != null)
                {
                    strSQL += " and UserHomeAddress='" + userEntity.HomeAddress + "'";
                }
                if (userEntity.LastLoginTime != "" && userEntity.LastLoginTime != null)
                {
                    strSQL += " and UserLastLoginTime='" + userEntity.LastLoginTime + "'";
                }
                if (userEntity.CreateUserID != -1)
                {
                    strSQL += " and UserCreateUserID=" + userEntity.CreateUserID + "";
                }
                if (userEntity.CreateDateTime != "" && userEntity.CreateDateTime != null)
                {
                    strSQL += " and UserCreateDate='" + userEntity.CreateDateTime + "'";
                }
                if (userEntity.ModifyUserID != -1)
                {
                    strSQL += " and UserModifyUserID=" + userEntity.ModifyUserID;
                }
                if (userEntity.ModifyDateTime != "" && userEntity.ModifyDateTime != null)
                {
                    strSQL += " and UserModifyDate='" + userEntity.ModifyDateTime + "'";
                }
                if (userEntity.RecordStatus != -1)
                {
                    strSQL += " and UserRecordStatus=" + userEntity.RecordStatus;
                }
                if (userEntity.Backup1 != null && userEntity.Backup1 != "")
                {
                    strSQL += " and UserBackup1='" + userEntity.Backup1 + "'";
                }
                if (userEntity.Backup2 != null && userEntity.Backup2 != "")
                {
                    strSQL += " and UserBackup1='" + userEntity.Backup2 + "'";
                }
                if (userEntity.Backup3 != null && userEntity.Backup3 != "")
                {
                    strSQL += " and UserBackup1='" + userEntity.Backup3 + "'";
                }
                if (userEntity.Backup4 != null && userEntity.Backup4 != "")
                {
                    strSQL += " and UserBackup1='" + userEntity.Backup4 + "'";
                }
                if (userEntity.Backup5 != null && userEntity.Backup5 != "")
                {
                    strSQL += " and UserBackup1='" + userEntity.Backup5 + "'";
                }
                //AppLog.Write(strSQL, AppLog.LogMessageType.Debug);
                dst = DAL_DBGM.GetDataSet(strSQL);
                List<Entity_USER_User> userList = new List<Entity_USER_User>();
                Entity_USER_User user = null;
                foreach (DataRow row in dst.Tables[0].Rows)
                {
                    user = new Entity_USER_User();
                    user.UserID = Convert.ToDecimal(row["UserID"]);
                    user.LoginName = Convert.ToString(row["UserLoginName"]);
                    user.LoginPassword = Convert.ToString(row["UserLoginPassword"]);
                    user.RealName = Convert.ToString(row["UserRealName"]);
                    user.Sex = Convert.ToString(row["UserSex"]);
                    user.MobilePhone = Convert.ToString(row["UserMobilePhone"]);
                    user.Telephony = Convert.ToString(row["UserTelephony"]);
                    user.Email = Convert.ToString(row["UserEmail"]);
                    user.QQ = Convert.ToString(row["UserQQ"]);
                    user.OfficeAddress = Convert.ToString(row["UserOfficeAddress"]);
                    user.HomeAddress = Convert.ToString(row["UserHomeAddress"]);
                    user.LastLoginTime = Convert.ToString(row["UserLastLoginTime"]);
                    user.CreateUserID = Convert.ToDecimal(row["UserCreateUserID"]);
                    user.CreateDateTime = Convert.ToString(row["UserCreateDate"]);
                    user.ModifyUserID = Convert.ToDecimal(row["UserModifyUserID"]);
                    user.ModifyDateTime = Convert.ToString(row["UserModifyDate"]);
                    user.RecordStatus = Convert.ToDecimal(row["UserRecordStatus"]);
                    user.Backup1 = Convert.ToString(row["UserBackup1"]);
                    user.Backup2 = Convert.ToString(row["UserBackup2"]);
                    user.Backup3 = Convert.ToString(row["UserBackup3"]);
                    user.Backup4 = Convert.ToString(row["UserBackup4"]);
                    user.Backup5 = Convert.ToString(row["UserBackup5"]);
                    userList.Add(user);
                }
                return userList;
            }
            catch (Exception ex)
            {
                AppLog.Write("---异常位置:LevelDataAccess.DAL_USER_User.getUserListByUserEntity(Entity_USER_User userEntity)---", AppLog.LogMessageType.Error, ex);
                throw ex;
            }
        }


        /// <summary>
        /// 增加一个用户.
        /// </summary>
        /// <param name="userEntity"></param>
        /// <returns>true:添加成功;false:添加失败</returns>
        public bool addUser(Entity_USER_User userEntity)
        {
            try
            {
                bool flag = false;
                bool blState = false;
                StringBuilder sqlBudIns = new StringBuilder();
                StringBuilder sqlBudValue = new StringBuilder();
                sqlBudIns.Append("insert into [PER_USER](");
                sqlBudValue.Append(" values(");
                List<SqlParameter> sprList = new List<SqlParameter>();
                if (userEntity.LoginName !="" && userEntity.LoginName != null)
                {
                    sqlBudIns.Append("UserLoginName,");
                    sqlBudValue.Append("@loginName,");
                    sprList.Add(new SqlParameter("@loginName",userEntity.LoginName));
                    blState = true;
                }
                //SqlParameter[] values = new SqlParameter[]{
                if (userEntity.LoginPassword != "" && userEntity.LoginPassword != null)
                {
                    sqlBudIns.Append("UserLoginPassword,");
                    sqlBudValue.Append("@loginPassword,");
                    sprList.Add(new SqlParameter("@loginPassword",userEntity.LoginPassword));
                    blState = true;
                }
                if (userEntity.RealName != "" && userEntity.RealName != null)
                {
                    sqlBudIns.Append("UserRealName,");
                    sqlBudValue.Append("@realName,");
                    sprList.Add(new SqlParameter("@realName",userEntity.RealName));
                    blState = true;
                }
                if (userEntity.Sex != "" && userEntity.Sex != null)
                {
                    sqlBudIns.Append("UserSex,");
                    sqlBudValue.Append("@sex,");
                    sprList.Add(new SqlParameter("@sex",userEntity.Sex));
                    blState = true;
                }
                if (userEntity.MobilePhone!="" && userEntity.MobilePhone!=null)
                {
                    sqlBudIns.Append("UserMobilePhone,");
                    sqlBudValue.Append("@mobilePhone,");
                    sprList.Add(new SqlParameter("@mobilePhone",userEntity.MobilePhone));
                    blState = true;
                }
                if (userEntity.Telephony!="" && userEntity.Telephony!=null)
                {
                    sqlBudIns.Append("UserTelephony,");
                    sqlBudValue.Append("@telePhony,");
                    sprList.Add(new SqlParameter("@telePhony",userEntity.Telephony));
                    blState = true;
                }
                if (userEntity.Email!="" && userEntity.Email!=null)
                {
                    sqlBudIns.Append("UserEmail,");
                    sqlBudValue.Append("@email,");                
                    sprList.Add(new SqlParameter("@email",userEntity.Email));
                    blState = true;
                }
                if (userEntity.QQ!="" && userEntity.QQ!=null)
                {
                    sqlBudIns.Append("UserQQ,");
                    sqlBudValue.Append("@qq,");
                    sprList.Add(new SqlParameter("@qq",userEntity.QQ));
                    blState = true;
                }
                if (userEntity.OfficeAddress!="" && userEntity.OfficeAddress!=null)
                {
                    sqlBudIns.Append("UserOfficeAddress,");
                    sqlBudValue.Append("@officeAddress,");
                    sprList.Add(new SqlParameter("@officeAddress",userEntity.OfficeAddress));
                    blState = true;
                }
                if (userEntity.HomeAddress!="" && userEntity.HomeAddress!=null)
                {
                    sqlBudIns.Append("UserHomeAddress,");
                    sqlBudValue.Append("@homeAddress,");
                    sprList.Add(new SqlParameter("@homeAddress",userEntity.HomeAddress));
                    blState = true;
                }
                if (userEntity.LastLoginTime!="" && userEntity.LastLoginTime!=null)
                {
                    sqlBudIns.Append("UserLastLoginTime,");
                    sqlBudValue.Append("@lastLoginTime,");
                    sprList.Add(new SqlParameter("@lastLoginTime",userEntity.LastLoginTime));
                    blState = true;
                }
                if (userEntity.CreateUserID != -1)
                {
                    sqlBudIns.Append("UserCreateUserID,");
                    sqlBudValue.Append("@createUserID,");
                    sprList.Add(new SqlParameter("@createUserID",userEntity.CreateUserID));
                    blState = true;
                }
                if (userEntity.CreateDateTime!="" && userEntity.CreateDateTime!=null)
                {
                    sqlBudIns.Append("UserCreateDate,");
                    sqlBudValue.Append("@createDateTime,");
                    sprList.Add(new SqlParameter("@createDateTime",userEntity.CreateDateTime));
                    blState = true;
                }
                if (userEntity.ModifyUserID != -1)
                {
                    sqlBudIns.Append("UserModifyUserID,");
                    sqlBudValue.Append("@modifyUserID,");
                    sprList.Add(new SqlParameter("@modifyUserID",userEntity.ModifyUserID));
                   blState = true;
                }              
                if (userEntity.ModifyDateTime!="" && userEntity.ModifyDateTime!=null)
                {
                    sqlBudIns.Append("UserModifyDate,");
                    sqlBudValue.Append("@modifyDateTime,");
                    sprList.Add( new SqlParameter("@modifyDateTime",userEntity.ModifyDateTime));
                    blState = true;
                }
                if (userEntity.RecordStatus != -1)
                {
                    sqlBudIns.Append("UserRecordStatus,");
                    sqlBudValue.Append("@recordStatus,");
                    sprList.Add(new SqlParameter("@recordStatus",userEntity.RecordStatus));
                    blState = true;
                }
                if (userEntity.Backup1!="" && userEntity.Backup1!=null)
                {
                    sqlBudIns.Append("UserBackup1,");
                    sqlBudValue.Append("@backup1,");
                    sprList.Add(new SqlParameter("@backup1",userEntity.Backup1));
                    blState = true;
                }
                if (userEntity.Backup2!="" && userEntity.Backup2!=null)
                {
                    sqlBudIns.Append("UserBackup2,");
                    sqlBudValue.Append("@backup2,");
                    sprList.Add(new SqlParameter("@backup2",userEntity.Backup2));
                    blState = true;
                }
                if (userEntity.Backup3!="" && userEntity.Backup3!=null)
                {
                    sqlBudIns.Append("UserBackup3,");
                    sqlBudValue.Append("@backup3,");
                    sprList.Add(new SqlParameter("@backup3",userEntity.Backup3));
                    blState = true;
                }
                if (userEntity.Backup4!="" && userEntity.Backup4!=null)
                {
                    sqlBudIns.Append("UserBackup4,");
                    sqlBudValue.Append("@backup4,");
                    sprList.Add(new SqlParameter("@backup4",userEntity.Backup4));
                    blState = true;
                }
                if (userEntity.Backup5!="" && userEntity.Backup5!=null)
                {
                    sqlBudIns.Append("UserBackup5,");
                    sqlBudValue.Append("@backup5,");
                    sprList.Add(new SqlParameter("@backup5",userEntity.Backup5));
                    blState = true;
                }

                if(blState)
                {
                    string strSQL = sqlBudIns.ToString().Substring(0,sqlBudIns.Length-1)+")" + sqlBudValue.ToString().Substring(0,sqlBudValue.Length-1)+")";
                    SqlParameter[] values = sprList.ToArray();
                    AppLog.Write("DAL-增加用户的SQL语句: " + strSQL, AppLog.LogMessageType.Debug); 
                    flag = DAL_DBGM.InsertInfo(strSQL, values);                   
                }
                else
                {
                    //添加用户失败:没有任何数据.
                    AppLog.Write("添加用户失败:没有任何数据.",AppLog.LogMessageType.Info);
                }
                // insert into [PER_USER](UserLoginName,UserLoginPassword@loginName,@loginPassword
                return flag;
            }
            catch (Exception ex)
            {
                AppLog.Write("---异常位置:LevelDataAccess.DAL_USER_User.addUser(Entity_USER_User userEntity)---", AppLog.LogMessageType.Error, ex);
                throw ex;
            }
        }

        /// <summary>
        /// 根据用户ID,修改用户资料.
        /// </summary>
        /// <param name="userID">待修改的用户ID</param>
        /// <param name="userEntity">待修改的用户资料</param>
        /// <returns>true:添加成功;false:添加失败</returns>
        public bool updateUserByUserID(decimal userID, Entity_USER_User userEntity)
        {
            try
            {
                bool flag = false;
                bool blState = false;
                string strSQL = "update [PER_USER] set";
                if (userEntity.LoginName != "" && userEntity.LoginName != null)
                {
                    strSQL += " UserLoginName='" + userEntity.LoginName + "',";
                    blState = true;
                }
                if (userEntity.LoginPassword != "" && userEntity.LoginPassword != null)
                {
                    strSQL += " UserLoginPassword='" + userEntity.LoginPassword + "',";
                    blState = true;
                }
                if (userEntity.RealName != "" && userEntity.RealName != null)
                {
                    strSQL += " UserRealName='" + userEntity.RealName + "',";
                    blState = true;
                }
                if (userEntity.Sex != "" && userEntity.Sex != null)
                {
                    strSQL += " UserSex='" + userEntity.Sex + "',";
                    blState = true;
                }
                if (userEntity.MobilePhone != "" && userEntity.MobilePhone != null)
                {
                    strSQL += " UserMobilePhone='" + userEntity.MobilePhone + "',";
                    blState = true;
                }
                if (userEntity.Telephony != "" && userEntity.Telephony != null)
                {
                    strSQL += " UserTelephony='" + userEntity.Telephony + "',";
                    blState = true;
                }
                if (userEntity.Email != "" && userEntity.Email != null)
                {
                    strSQL += " UserEmail='" + userEntity.Email + "',";
                    blState = true;
                }
                if (userEntity.QQ != "" && userEntity.QQ != null)
                {
                    strSQL += " UserQQ='" + userEntity.QQ + "',";
                    blState = true;
                }
                if (userEntity.OfficeAddress != "" && userEntity.OfficeAddress != null)
                {
                    strSQL += " UserOfficeAddress='" + userEntity.OfficeAddress + "',";
                    blState = true;
                }
                if (userEntity.HomeAddress != "" && userEntity.HomeAddress != null)
                {
                    strSQL += " UserHomeAddress='" + userEntity.HomeAddress + "',";
                    blState = true;
                }
                if (userEntity.LastLoginTime != "" && userEntity.LastLoginTime != null)
                {
                    strSQL += " UserLastLoginTime='" + userEntity.LastLoginTime + "',";
                    blState = true;
                }
                if (userEntity.CreateUserID != -1)
                {
                    strSQL += " UserCreateUserID=" + userEntity.CreateUserID + ",";
                    blState = true;
                }
                if (userEntity.CreateDateTime != "" && userEntity.CreateDateTime != null)
                {
                    strSQL += " UserCreateDate='" + userEntity.CreateDateTime + "',";
                    blState = true;
                }
                if (userEntity.ModifyUserID != -1)
                {
                    strSQL += " UserModifyUserID=" + userEntity.ModifyUserID +",";
                    blState = true;
                }
                if (userEntity.ModifyDateTime != "" && userEntity.ModifyDateTime != null)
                {
                    strSQL += " UserModifyDate='" + userEntity.ModifyDateTime + "',";
                    blState = true;
                }
                if (userEntity.RecordStatus != -1)
                {
                    strSQL += " UserRecordStatus=" + userEntity.RecordStatus+",";
                    blState = true;
                }
                if (userEntity.Backup1 != null && userEntity.Backup1 != "")
                {
                    strSQL += " UserBackup1='" + userEntity.Backup1 + "',";
                    blState = true;
                }
                if (userEntity.Backup2 != null && userEntity.Backup2 != "")
                {
                    strSQL += " UserBackup2='" + userEntity.Backup2 + "',";
                    blState = true;
                }
                if (userEntity.Backup3 != null && userEntity.Backup3 != "")
                {
                    strSQL += " UserBackup3='" + userEntity.Backup3 + "',";
                    blState = true;
                }
                if (userEntity.Backup4 != null && userEntity.Backup4 != "")
                {
                    strSQL += " UserBackup4='" + userEntity.Backup4 + "',";
                    blState = true;
                }
                if (userEntity.Backup5 != null && userEntity.Backup5 != "")
                {
                    strSQL += " UserBackup5='" + userEntity.Backup5 + "',";
                    blState = true;
                }
                if(blState)
                {
                    strSQL = strSQL.Substring(0,strSQL.Length - 1) + " where UserID = @userID";
                    SqlParameter[] values = new SqlParameter[]{
                        new SqlParameter("@userID",userID)
                    };
                    //AppLog.Write(strSQL, AppLog.LogMessageType.Debug);
                    flag = DAL_DBGM.UpdateInfo(strSQL,values);
                    return flag;
                }
                else
                {
                    //没有需要更新的内容.
                    return flag;
                }                

            }
            catch (Exception ex)
            {
                AppLog.Write("---异常位置:LevelDataAccess.DAL_USER_User.updateUserByUserID(decimal userID, Entity_USER_User userEntity)---", AppLog.LogMessageType.Error, ex);
                throw ex;
            }
        }


        /// <summary>
        /// 根据用户ID,删除指定用户.
        /// </summary>
        /// <param name="userID">要删除的用户ID</param>
        /// <returns>true:删除成功;false:删除失败</returns>
        public bool deleteUserByUserID(decimal userID)
        {
            try
            {
                bool flag = false;
                string strSQL = "delete from [PER_USER] where UserID="+userID;
                //SqlParameter[] values = new SqlParameter[]{
                //    new SqlParameter("@userID",userID)
                //};
                //flag = DAL_DBGM.DeleteInfo(strSQL,values);
                flag = DAL_DBGM.DeleteInfo(strSQL);
                AppLog.Write("deleteDAL-"+strSQL+"-", AppLog.LogMessageType.Debug);
                return flag;
            }
            catch (System.Exception ex)
            {
                AppLog.Write("---异常位置:LevelDataAccess.DAL_USER_User.deleteUserByUserID(decimal userID))---", AppLog.LogMessageType.Error, ex);
                throw ex;
            }
        }

        /// <summary>
        /// 获得分页数据
        /// </summary>
        /// <param name="user"></param>
        /// <param name="currentPage"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public List<Entity_USER_User> getUserPaginationList(Entity_USER_User cuser,int currentPage,int pageSize)
        {
            string sql = @"SELECT TOP {0} * FROM PER_USER WHERE (UserID NOT IN (SELECT TOP {1} UserID FROM PER_USER WHERE 1=1 {2} ORDER BY UserID)) {3} ORDER BY UserID"
                    .FormatWith(pageSize, (currentPage - 1) * pageSize, getConditionSql(cuser), getConditionSql(cuser));
            DataSet dst = DAL_DBGM.GetDataSet(sql);
            List<Entity_USER_User> userList = new List<Entity_USER_User>();
            Entity_USER_User user = null;
            foreach (DataRow row in dst.Tables[0].Rows)
            {
                user = new Entity_USER_User();
                user.UserID = Convert.ToDecimal(row["UserID"]);
                user.LoginName = Convert.ToString(row["UserLoginName"]);
                user.LoginPassword = Convert.ToString(row["UserLoginPassword"]);
                user.RealName = Convert.ToString(row["UserRealName"]);
                user.Sex = Convert.ToString(row["UserSex"]);
                user.MobilePhone = Convert.ToString(row["UserMobilePhone"]);
                user.Telephony = Convert.ToString(row["UserTelephony"]);
                user.Email = Convert.ToString(row["UserEmail"]);
                user.QQ = Convert.ToString(row["UserQQ"]);
                user.OfficeAddress = Convert.ToString(row["UserOfficeAddress"]);
                user.HomeAddress = Convert.ToString(row["UserHomeAddress"]);
                user.LastLoginTime = Convert.ToString(row["UserLastLoginTime"]);
                user.CreateUserID = Convert.ToDecimal(row["UserCreateUserID"]);
                user.CreateDateTime = Convert.ToString(row["UserCreateDate"]);
                user.ModifyUserID = Convert.ToDecimal(row["UserModifyUserID"]);
                user.ModifyDateTime = Convert.ToString(row["UserModifyDate"]);
                user.RecordStatus = Convert.ToDecimal(row["UserRecordStatus"]);
                user.Backup1 = Convert.ToString(row["UserBackup1"]);
                user.Backup2 = Convert.ToString(row["UserBackup2"]);
                user.Backup3 = Convert.ToString(row["UserBackup3"]);
                user.Backup4 = Convert.ToString(row["UserBackup4"]);
                user.Backup5 = Convert.ToString(row["UserBackup5"]);
                userList.Add(user);
            }
            return userList;
        }

        /// <summary>
        /// 获得用户数
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public int getAllCount(Entity_USER_User user)
        {
            string sql = "select count(*) COUNT from PER_USER where 1=1 {0}".FormatWith(this.getConditionSql(user));
            return Convert.ToInt16(DAL_DBGM.GetDataSet(sql).Tables[0].Rows[0]["COUNT"]);
        }

        private string getConditionSql(Entity_USER_User user)
        {
            if (user == null) return "";
            StringBuilder whereSql = new StringBuilder();
            if(!user.RealName.IsNullEmpty())
            {
                whereSql.Append(" and UserRealName like '%").Append(user.RealName).Append("%'");
            }

            if (!user.MobilePhone.IsNullEmpty())
            {
                whereSql.Append(" and UserMobilePhone = '").Append(user.MobilePhone).Append("'");
            }

            if (!user.Telephony.IsNullEmpty())
            {
                whereSql.Append(" and UserTelephony = '").Append(user.Telephony).Append("'");
            }

            return whereSql.ToString();
        }

    }

}
